package org.zw.android.framework.impl;

import org.zw.android.framework.IBitmapDownloader;
import org.zw.android.framework.cache.IDownloaderCallback;
import org.zw.android.framework.cache.ImageDownloader;
import org.zw.android.framework.cache.ImageDownloader.State;
import org.zw.android.framework.cache.ImageOption;
import org.zw.android.framework.cache.RecyclingImageView;

import android.content.Context;
import android.graphics.Bitmap;

/**
 * Asynchronous loading Bitmap Implements
 * 
 * @author zhouwei
 *
 */
public final class BitmapDownloaderImpl implements IBitmapDownloader {
	
	private static BitmapDownloaderImpl mInstance ;
	private ImageDownloader mCacheManager ;
	private Context mContext ;
	
	private BitmapDownloaderImpl(Context context,String cacheName,float percent,int maxWidth,int maxHeight){
		
		if(cacheName == null){
			throw new RuntimeException("BitmapDownloaderImpl CacheName is null");
		}
		
		mContext 	  = context ;
		mCacheManager = ImageDownloader.create(context, cacheName, percent < 0.01f ? 0.01f : percent,maxWidth,maxHeight);
	}
	
	protected static BitmapDownloaderImpl initImageCache(Context context,
				String cacheName,
				float percent,
				int maxWidth,
				int maxHeight){
		
		if(mInstance == null){
			mInstance = new BitmapDownloaderImpl(context, cacheName, percent, maxWidth, maxHeight);
		}
		
		return mInstance ;
	}
	
	public static IBitmapDownloader getBitmapDownloader(){
		return mInstance ;
	}
	
	@Override
	public Context getContext() {
		return mContext;
	}

	@Override
	public void downloadBitmap(String url, IDownloaderCallback callback) {
		
		ImageOption op = new ImageOption() ;
		op.setCallback(callback);
		
		mCacheManager.downloadBitmap(url, op);
	}

	@Override
	public void downloadBitmap(String url, RecyclingImageView imageView,
			int rid, ImageOption op) {
		mCacheManager.downloadBitmap(url,imageView,rid, op);
	}

	@Override
	public void downloadBitmap(String url, RecyclingImageView imageView,
			Bitmap defaultBitmap, ImageOption op) {
		mCacheManager.downloadBitmap(url,imageView,defaultBitmap, op);
	}
	
	@Override
	public void downloadBitmap(String url, ImageOption op) {
		mCacheManager.downloadBitmap(url, op);
	}

	@Override
	public void downloadBitmap(String url, RecyclingImageView imageView,ImageOption op) {
		downloadBitmap(url,imageView,null,op);
	}
	
	@Override
	public void downloadBitmap(String url, RecyclingImageView imageView, int rid) {
		downloadBitmap(url,imageView,rid,ImageOption.defOption);
	}

	@Override
	public Bitmap getBitmap(String url) {
		return mCacheManager != null ? mCacheManager.getBitmapFromCache(url) : null;
	}

	@Override
	public Bitmap getBitmap(String url, ImageOption op) {
		return mCacheManager != null ? mCacheManager.getBitmapFromCache(url,op) : null;
	}

	@Override
	public String getCacheLocalPath(String url) {
		return mCacheManager != null ? mCacheManager.getCacheLocalPath(url) : null;
	}

	@Override
	public void onStart() {
		if(mCacheManager != null){
			mCacheManager.onStart() ;
		}
	}

	@Override
	public void onPause() {
		if(mCacheManager != null){
			mCacheManager.onPause() ;
		}
	}

	@Override
	public void onResume() {
		if(mCacheManager != null){
			mCacheManager.onResume() ;
		}
	}

	@Override
	public void onDestory() {
		if(mCacheManager != null){
			mCacheManager.onDestory() ;
		}
	}

	@Override
	public void setState(State newState) {
		if(mCacheManager != null){
			mCacheManager.setState(newState) ;
		}
	}

	@Override
	public State getState() {
		return mCacheManager != null ? mCacheManager.getState() : State.STATE_OFF ;
	}

	@Override
	public void clearDiskCache() {
		if(mCacheManager != null){
			mCacheManager.clearDiskCacheFile() ;
		}
	}

	@Override
	public void onReset() {
		if(mCacheManager != null){
			mCacheManager.onReset() ;
		}
	}

	@Override
	public void onCancelTask() {
		if(mCacheManager != null){
			mCacheManager.onCancelTask() ;
		}
	}
	
}
